package com.xxrl.hmwork.service;

import com.xxrl.hmwork.domain.Homework;
import com.xxrl.hmwork.repository.HomeworkRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


/**
 * @author mis
 */
@Service
public class HomeworkService {

    private final HomeworkRepository homeworkRepository;

    @Autowired
    public HomeworkService(HomeworkRepository homeworkRepository) {
        this.homeworkRepository = homeworkRepository;
    }

    public boolean validate(Long id) {
        return homeworkRepository.existsById(id);
    }

    public Page<Homework> list(Pageable pageable) {
        return homeworkRepository.findAll(pageable);
    }

    public void save(Homework homework) {
        // 创建或保存
        homeworkRepository.save(homework);
    }

    public void delete(Long id) {
        homeworkRepository.deleteById(id);
    }

    public Homework query(String title) {
        return homeworkRepository.findFirstByTitle(title);
    }
}
